package com.diko.product.app;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.diko.product.entity.AttrEntity;
import com.diko.product.service.AttrAttrgroupRelationService;
import com.diko.product.service.AttrService;
import com.diko.product.service.CategoryService;
import com.diko.product.vo.AttrGroupRelationVo;
import com.diko.product.vo.AttrGroupWithAttrsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.diko.product.entity.AttrGroupEntity;
import com.diko.product.service.AttrGroupService;
import com.diko.common.utils.PageUtils;
import com.diko.common.utils.R;



/**
 * 属性分组
 *
 * @author Emma Li
 * @email 1006298386@qq.com
 * @date 2021-04-18 15:02:18
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private AttrService attrService;

    @Autowired
    private AttrAttrgroupRelationService attrAttrgroupRelationService;

    /**
     * 获取分类下所有分组&关联属性
     * @param catelogId
     * @return
     * */
    @GetMapping("/{catelogId}/withattr")
    public R getAttrGroupWithAttrs(@PathVariable("catelogId")Long catelogId){
        //1.查出当前分类下的所有属性分组
        //2.查出每个属性分组的所有属性
        List<AttrGroupWithAttrsVo> vos=attrGroupService.getAttrGroupWithAttrsByCatelogId(catelogId);
        return R.ok().put("data",vos);
    }

    /**
     * 获取分组关联的属性
     * @param attrgroupId
     * @return
     * */
    @GetMapping("/{attrgroupId}/attr/relation")
    public R attrRelation(@PathVariable("attrgroupId") Long attrgroupId){
        List<AttrEntity> entityList=attrService.getAttrRelation(attrgroupId);

        return R.ok().put("data",entityList);
    }

    /**
     * 添加属性与分组关联关系
     * @param vos
     * @return
     * */
    @PostMapping("/attr/relation")
    public R addRelation(@RequestBody List<AttrGroupRelationVo> vos){
        attrAttrgroupRelationService.saveBatch(vos);
        return R.ok();
    }

    /**
     * 删除顺序与分组的关联关系
     * (因为需要填写多条信息，则是@ResponseBody而不是@PathVariable)
     * @param
     * @return
     * */
    @PostMapping("attr/relation/delete")
    public R deleteAttrRelation(@RequestBody AttrGroupRelationVo[] attrGroupRelationVos){
        //是在attrService里删除（规格属性）
        attrService.deleteAttrRelation(attrGroupRelationVos);
        return R.ok();
    }

    /**
     * 获取分组未关联的属性
     * @param attrgroupId
     * @param params
     * @return
     * */
    @GetMapping("/{attrgroupId}/noattr/relation")
    public R attrNoRelation(@PathVariable("attrgroupId") Long attrgroupId,
                            @RequestParam Map<String,Object> params){
        PageUtils page=attrService.getNoRelationAttr(params,attrgroupId);
        return R.ok().put("data",page);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("product:attrgroup:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = attrGroupService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 列表
     */
    @RequestMapping("/list/{catelogId}")
    //@RequiresPermissions("product:attrgroup:list")
    public R listById(@RequestParam Map<String, Object> params,
                      @PathVariable("catelogId") Long catelogId){
        //不满足我们的要求，我们自定义一个queryPage方法，传入params和catelogId两个参数
//        PageUtils page = attrGroupService.queryPage(params);
        PageUtils page = attrGroupService.queryPage(params,catelogId);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     * 根据三级分类id，找到三级分类的完整路径
     */
    @RequestMapping("/info/{attrGroupId}")
    //@RequiresPermissions("product:attrgroup:info")
    public R info(@PathVariable("attrGroupId") Long attrGroupId){
		AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);

		//通过上面的方法获得了分组信息后，还要把三级分类路径加进去set
		Long[] catelogPath=categoryService.findCatelogPath(attrGroup.getCatelogId());
		attrGroup.setCatelogPath(catelogPath);
        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:attrgroup:save")
    public R save(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:attrgroup:update")
    public R update(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:attrgroup:delete")
    public R delete(@RequestBody Long[] attrGroupIds){
		attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

}
